package com.example.doer.reading.AppActivities;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;

import com.ab.activity.AbActivity;
import com.ab.http.AbHttpUtil;
import com.ab.http.AbStringHttpResponseListener;
import com.ab.util.AbDialogUtil;
import com.example.doer.reading.AppDataModel.AppUser;
import com.example.doer.reading.MyCarrousel.CarrouselLayout;
import com.example.doer.reading.MyCarrousel.OnCarrouselItemClickListener;
import com.example.doer.reading.MyCarrousel.OnCarrouselItemSelectedListener;
import com.example.doer.reading.R;
import com.example.doer.reading.global.MyApplication;

import static com.ab.util.AbToastUtil.showToast;

public class test2Activity extends AbActivity {

    private final  String TAG=MainActivity.class.getSimpleName();
    private int width;
    private CarrouselLayout carrousel;

    private boolean []isSelected=new boolean[9];
    private String [] categorys;
    TextView selectText;
    private AbHttpUtil abHttpUtil=null;
    private AppUser appUser;

    private MyApplication application;
    private void initView() {
        selectText= (TextView) findViewById(R.id.Has_select);
        categorys=getResources().getStringArray(R.array.categorys);
        carrousel= (CarrouselLayout) findViewById(R.id.carrousel);//R:175 RotationZ:5 ScaleX:2 ScaleY:2.7
        for (int i=0;i<isSelected.length;i++){
            isSelected[i]=false;
        }

//                <!--<ImageView-->
//            <!--android:src="@mipmap/image4"-->
//            <!--android:layout_width="wrap_content"-->
//            <!--android:layout_height="wrap_content" />-->

//        ImageView imageView=new ImageView(test2Activity.this);
//        imageView.setImageResource(R.mipmap.image6);
//        imageView.setBackgroundResource(R.drawable.image_select);
//        imageView.setClickable(true);
//        carrousel.addView(imageView);
//        View view=new View(test2Activity.this);

//        view.setLayoutParams(R.layout.booklayout);
//        view=findViewById(R.id.myList);
//        ImageView image= (ImageView) view.findViewById(R.id.imageView5);
//        TextView textView= (TextView) view.findViewById(R.id.tv_InfoList);
//        ImageView image =new ImageView(test2Activity.this);
//        TextView textView=new TextView(test2Activity.this);
//        ArrayList<View>list=new ArrayList<View>();
//        list.add(image);
//        list.add(textView);
//        view.addChildrenForAccessibility(list);
//        image.setImageResource(R.mipmap.image7);
//        textView.setText("hello");
//        carrousel.addView(view);

        carrousel.setScaleX(2);//设置尺寸
        carrousel.setScaleY(2);

        carrousel.setRotationX(-25);
        carrousel.setRotationY(0);
        carrousel.setRotationZ(0);


        DisplayMetrics dm = new DisplayMetrics();
        WindowManager windowManager = (WindowManager) this.getSystemService(Context.WINDOW_SERVICE);
        windowManager.getDefaultDisplay().getMetrics(dm);
        width=dm.widthPixels;
        carrousel.setR((float) (width/6))//设置R的大小
                .setAutoRotation(false)//是否自动切换
                .setAutoRotationTime(1500);//自动切换的时间  单位毫秒
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setAbContentView(R.layout.activity_test2);
        application= (MyApplication) abApplication;
        appUser= (AppUser) getIntent().getSerializableExtra("user");
        abHttpUtil=AbHttpUtil.getInstance(this);
        initView();
        initLinstener();

        TextView changeText=(TextView)findViewById(R.id.textView9);
        changeText.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                test2Activity.this.recreate();
            }
        });
//        SimpleDateFormat sdf;
//        final String date;
//        sdf = new SimpleDateFormat("yyyy-mm-dd");//HH:mm为24小时制,hh:mm为12小时制
//        date = sdf.format(new java.util.Date());
        Button button_next= (Button) findViewById(R.id.button9);

        button_next.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (selectText.getText().toString().equals("")){
                    showToast(test2Activity.this,"选一个吧");
                    return;
                }

                String url="http://192.168.191.1/Reading/InfoDoServlet?Action=register&userID="+appUser.getUserID()+"&userName="+appUser.getUserName()+
                        "&userJob="+appUser.getUserJob()+"&userSchool="+appUser.getUserSchool()+"&description="+appUser.getUserDescription()+"&sex="+appUser.getUserSex()
                        +"&value="+selectText.getText().toString();
                abHttpUtil.get(url, new AbStringHttpResponseListener() {
                    @Override
                    public void onSuccess(int i, String s) {
                        if (s.equals("true")){
                            appUser.setLoginUser(true);
                            application.updateLoginParams(appUser);
                            //上传数据库
                            application.setFirstStart(false);
                            startActivity(new Intent(test2Activity.this,MainActivity.class));
                            Log.e("Select Topic","成功选择");
                            AbDialogUtil.removeDialog(test2Activity.this);
                        }else
                            showToast(test2Activity.this,"注册失败,是否已有账户,去登录？");
                        //不能写在完成中，因为完成在有重定向的情况会提前结束
                        AbDialogUtil.removeDialog(test2Activity.this);

                    }

                    @Override
                    public void onStart() {
                        AbDialogUtil.showProgressDialog(test2Activity.this,0,"正在执行...");
                    }
                    @Override
                    public void onFinish() {

                    }
                    @Override
                    public void onFailure(int i, String s, Throwable throwable) {
                        AbDialogUtil.removeDialog(test2Activity.this);
                        showToast(test2Activity.this,throwable.getMessage());
                    }
                });
            }
        });
    }
    @Override
    protected void onResume() {
        super.onResume();
        //开启自动
        carrousel.resumeAutoRotation();
    }


    @Override
    protected void onStop() {
        super.onStop();
        //停止自动
        carrousel.stopAutoRotation();
    }

    private void initLinstener() {
        carrousel.setOnCarrouselItemClickListener(new OnCarrouselItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                if (!isSelected[position]){
                    isSelected[position]=true;
//                    String before=selectText.getText().toString();
                    selectText.append(categorys[position]+",");
                    selectText.refreshDrawableState();
                }else {
                    isSelected[position]=false;
                    String s=selectText.getText().toString();
                    s=s.replaceAll(categorys[position]+",","");
                    selectText.setText(s);
                    selectText.refreshDrawableState();
                }
            }
        });
        /**
         * 选中回调
         */
        carrousel.setOnCarrouselItemSelectedListener(new OnCarrouselItemSelectedListener() {

            @Override
            public void selected(View view, int position) {
                Log.v(TAG,"position:"+position);
            }
        });

    }
}
